package com.micangcaihang.micang.adapter;

import android.content.Context;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.view.View;
import android.view.ViewGroup;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.chad.library.adapter.base.BaseMultiItemQuickAdapter;
import com.chad.library.adapter.base.BaseQuickAdapter;
import com.chad.library.adapter.base.BaseViewHolder;
import com.micangcaihang.common.utils.ALog;
import com.micangcaihang.common.utils.ConvertUtils;
import com.micangcaihang.common.utils.StringUtils;
import com.micangcaihang.common.widget.BetterRecyclerView;
import com.micangcaihang.common.widget.banner.OnNoDoubleClickListener;
import com.micangcaihang.micang.R;
import com.micangcaihang.micang.entity.TaskAccessoryInfoEntity;

import net.cachapa.expandablelayout.ExpandableLayout;

import java.util.List;

/**
 * <pre>
 *     author : Fandy
 *     e-mail : fandayongfly@gmail.com
 *     time   : 2017/08/06
 *     update : 2017/08/06
 *     desc   : 附件信息适配器(通用)
 *     version: 1.0
 * </pre>
 */

public class AccessoryInfoAdapter extends BaseMultiItemQuickAdapter<TaskAccessoryInfoEntity, BaseViewHolder> {
    private Context mContext;
    private static final int TYPE_TITLE = 1;
    private static final int TYPE_CONTENT = 0;

    /**
     * 点击添加图片跳转
     */
    private onAddPicClickListener mOnAddPicClickListener;
    protected OnItemClickListener mItemClickListener;
    private DPictureAdapter mDPictureAdapter;
    private boolean isAddType = true;
    private boolean isExpandable = true;

    /**
     * Same as QuickAdapter#QuickAdapter(Context,int) but with
     * some initialization data.
     *
     * @param data A new list is created out of this one to avoid mutable list
     */
    public AccessoryInfoAdapter(Context context, List<TaskAccessoryInfoEntity> data) {
        super(data);
        this.mContext = context;
        addItemType(TYPE_TITLE, R.layout.item_accessory_title);
        addItemType(TYPE_CONTENT, R.layout.item_accessory_info);
    }

    /**
     * Same as QuickAdapter#QuickAdapter(Context,int) but with
     * some initialization data.
     *
     * @param data A new list is created out of this one to avoid mutable list
     */
    public AccessoryInfoAdapter(Context context, List<TaskAccessoryInfoEntity> data, boolean isAddType) {
        super(data);
        this.isAddType = isAddType;
        this.mContext = context;
        addItemType(TYPE_TITLE, R.layout.item_accessory_title);
        addItemType(TYPE_CONTENT, R.layout.item_accessory_info);
    }

    /**
     * Same as QuickAdapter#QuickAdapter(Context,int) but with
     * some initialization data.
     *
     * @param data A new list is created out of this one to avoid mutable list
     */
    public AccessoryInfoAdapter(Context context, List<TaskAccessoryInfoEntity> data, boolean isAddType, boolean isExpandable) {
        super(data);
        this.isAddType = isAddType;
        this.isExpandable = isExpandable;
        this.mContext = context;
        addItemType(TYPE_TITLE, R.layout.item_accessory_title);
        addItemType(TYPE_CONTENT, R.layout.item_accessory_info);
    }


    public interface onAddPicClickListener {
        void onAddPicClick(BaseQuickAdapter adapter, int parentPos, int position, View v);

        void onAddPicClick(int layoutPosition);
    }

    public interface OnItemClickListener {
        void onItemClick(BaseQuickAdapter adapter, int parentPos, int position, View v);
    }

    public void setOnItemClickListener(OnItemClickListener listener) {
        this.mItemClickListener = listener;
    }

    public void setOnAddPicClickListener(onAddPicClickListener onAddPicClickListener) {
        mOnAddPicClickListener = onAddPicClickListener;
    }

    public void setExpandable(boolean expandable) {
        isExpandable = expandable;
    }

    public boolean isExpandable() {
        return isExpandable;
    }

    @Override
    protected void convert(BaseViewHolder helper, TaskAccessoryInfoEntity item) {
        int layoutPosition;
        if (getHeaderLayoutCount() > 0) {
            layoutPosition = helper.getLayoutPosition() - getHeaderLayoutCount();
        } else {
            layoutPosition = helper.getLayoutPosition();
        }
        switch (helper.getItemViewType()) {
            case TYPE_TITLE:
                helper.setText(R.id.tv_big_title, item.getBigtitle());
                break;
            case TYPE_CONTENT:
                int file_category = item.getRequire_flag();
                try {
                    if (file_category > 0) {
                        helper.setVisible(R.id.tv_must, true);
                    } else {
                        helper.setVisible(R.id.tv_must, false);
                    }
                } catch (Exception e) {
                    helper.setVisible(R.id.tv_must, false);
                }
//                if (item.getFile_content().size() > 1) {
//                    helper.setVisible(R.id.lly_item_small_title, true);
//                } else {
//                    helper.setVisible(R.id.lly_item_small_title, false);
//                }
                TextView tv_file_type_name = helper.getView(R.id.tv_file_type_name);
                if (!StringUtils.isEmpty(item.getFile_type_name())) {
                    tv_file_type_name.setText(item.getFile_type_name());
                } else if (!StringUtils.isEmpty(item.getFile_name())) {
                    tv_file_type_name.setText(item.getFile_name());
                } else {
                    helper.setVisible(R.id.lly_item_small_title, false);
                }
                TextView tv_upload = helper.getView(R.id.tv_upload);
                helper.addOnClickListener(R.id.tv_upload);
                if (isAddType) {
                    tv_upload.setVisibility(View.VISIBLE);
                    tv_upload.setOnClickListener(new OnNoDoubleClickListener() {
                        @Override
                        public void onNoDoubleClick(View v) {
                            if (mOnAddPicClickListener != null) {
                                mOnAddPicClickListener.onAddPicClick(layoutPosition);
                            }
                        }
                    });
                } else {
                    tv_upload.setVisibility(View.GONE);
                }
                BetterRecyclerView recyclerView = helper.getView(R.id.recycler_view);
                recyclerView.setLayoutManager(new LinearLayoutManager(mContext, LinearLayoutManager.HORIZONTAL, false));
                recyclerView.setHasFixedSize(true);
                mDPictureAdapter = new DPictureAdapter(R.layout.item_img, item.getFile_content(), isAddType);
                mDPictureAdapter.setOnItemClickListener((adapter, view, position) -> {
                    if (isAddType && position == adapter.getItemCount() - 1) {
                        if (mOnAddPicClickListener != null) {
                            mOnAddPicClickListener.onAddPicClick(adapter, layoutPosition, position, view);
                        }
                    } else {
                        if (mItemClickListener != null) {
                            mItemClickListener.onItemClick(adapter, layoutPosition, position, view);
                        }
                    }
                });
                recyclerView.setAdapter(mDPictureAdapter);
                if (isAddType) {
                    recyclerView.addOnScrollListener(new RecyclerView.OnScrollListener() {
                        //用来标记是否正在向最后一个滑动
                        boolean isSlidingToLast = false;

                        @Override
                        public void onScrollStateChanged(RecyclerView recyclerView, int newState) {
                            super.onScrollStateChanged(recyclerView, newState);
                            LinearLayoutManager manager = (LinearLayoutManager) recyclerView.getLayoutManager();
                            // 当不滚动时
                            if (newState == RecyclerView.SCROLL_STATE_IDLE) {
                                int firstCompletelyVisibleItemPosition = manager.findFirstCompletelyVisibleItemPosition();
                                int lastVisibleItemPosition = manager.findLastCompletelyVisibleItemPosition();
                                ALog.e( recyclerView.getAdapter().getItemCount()+"");
                                if (firstCompletelyVisibleItemPosition != 0 && lastVisibleItemPosition != recyclerView.getAdapter().getItemCount() - 1) {
                                    tv_upload.setVisibility(View.VISIBLE);
                                } else if (firstCompletelyVisibleItemPosition == 0 && lastVisibleItemPosition == recyclerView.getAdapter().getItemCount() - 1) {
                                    tv_upload.setVisibility(View.GONE);
                                } else if (firstCompletelyVisibleItemPosition == 0 && lastVisibleItemPosition != recyclerView.getAdapter().getItemCount() - 1) {
                                    tv_upload.setVisibility(View.VISIBLE);
                                } else {
                                    tv_upload.setVisibility(View.GONE);
                                }
                            }
                        }

                        @Override
                        public void onScrolled(RecyclerView recyclerView, int dx, int dy) {
                            super.onScrolled(recyclerView, dx, dy);
                            LinearLayoutManager manager = (LinearLayoutManager) recyclerView.getLayoutManager();
                            int firstCompletelyVisibleItemPosition = manager.findFirstCompletelyVisibleItemPosition();
                            int lastVisibleItemPosition = manager.findLastCompletelyVisibleItemPosition();
                            if (firstCompletelyVisibleItemPosition != 0 && lastVisibleItemPosition != recyclerView.getAdapter().getItemCount() - 1) {
                                tv_upload.setVisibility(View.VISIBLE);
                            } else if (firstCompletelyVisibleItemPosition == 0 && lastVisibleItemPosition == recyclerView.getAdapter().getItemCount() - 1) {
                                tv_upload.setVisibility(View.GONE);
                            } else if (firstCompletelyVisibleItemPosition == 0 && lastVisibleItemPosition != recyclerView.getAdapter().getItemCount() - 1) {
                                tv_upload.setVisibility(View.VISIBLE);
                            } else {
                                tv_upload.setVisibility(View.GONE);
                            }
                        }
                    });
                }
                ExpandableLayout view1 = helper.getView(R.id.expandable_layout);
                if (isExpandable) {
                    view1.expand();
                } else {
                    view1.collapse();
                }
                break;
        }
        LinearLayout.LayoutParams params = new LinearLayout.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.WRAP_CONTENT);
        int dp_89 = ConvertUtils.dp2px(89);
        int dp_22 = ConvertUtils.dp2px(22);
        if (helper.getLayoutPosition() == getItemCount() - 1) {
            params.setMargins(0, 0, 0, 22);
        } else {
            params.setMargins(0, 0, 0, 0);
        }

        helper.itemView.setLayoutParams(params);
    }
}
